<%@ Page language="vb" CodeFile="recordInvoice.aspx.vb" Inherits="recordInvoice_aspx_vb" %>

<%
    rowNumber = 0
    rowCounter = - 1
    pageCounter = 2
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
    Over()
    strJS = ""
    strJS = strJS + "[""companyID"", ""<1""],"
    strJS = strJS + "[""invoiceDate"", ""==''""]"
    checkFields()
    Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
    Response.Write("<tr>")
    Response.Write("<td valign='top'>")
    Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
    Response.Write("<tr><td class='headline' colspan='3'>INVOICE RECORD<hr class='hrHeadline'></td></tr>")
    Response.Write("<form action='recordInvoice.aspx?do=print' method='post' target='_blank' onsubmit=""javascript:return checkFields();"">")
    Response.Write("<tr>")
    Response.Write("<td class='formBold'>Company:</td>")
    Response.Write("<td align='right'>")
    Response.Write("<select name='companyID' id='companyID'>")
    Response.Write("<option value=''> Select company  ")
    dbOpen()
    RecSet = Conn.Execute("Select companyID, company From tblCompany")
    If Not RecSet.EOF Then 
        Do Until RecSet.EOF
            companyID = RecSet.Fields("companyID").Value
            company = RecSet.Fields("company").Value
            Response.Write("<option value='" & companyID & "'> " & company & " ")
            RecSet.MoveNext()
        Loop
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    Response.Write("<option value=''> -------------------------------------- ")
    Response.Write("</select>")
    Response.Write("</td>")
    Response.Write("<td><div id='error_companyID' class='edit_error'>&laquo; OBS!</div></td>")
    Response.Write("</tr>")
    Response.Write("<tr><td class='formBold'>Latest invoice date:</td><td align='right'><input type='text' name='invoiceDate' id='invoiceDate' size='40' maxlength='10' onkeyup='re=/([\d\-]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_invoiceDate' class='edit_error'>&laquo; OBS!</div></td></tr>")
    Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Search'></td></tr>")
    Response.Write("</form>")
    Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
    Response.Write("</table>")
    Response.Write("</td>")
    Response.Write("</tr>")
    Response.Write("</table>")
ElseIf Request.QueryString("do") = "print" Then 
    companyID = Request.Form("companyID")
    qInvoiceDate = Request.Form("invoiceDate")
    companyID = CLng(companyID)
    debitSubtotal = 0
    creditSubtotal = 0
    roundingSubtotal = 0
    taxSubtotal = 0
    debitGrandtotal = 0
    creditGrandtotal = 0
    roundingGrandtotal = 0
    taxGrandtotal = 0
    uwGrandTotal = 0
%>
<script type="text/javascript">
  function printCheck() { 
	if (window.confirm('OBS! Please comfirm that the printouts are OK.')) {
		location.href='recordInvoice.aspx?do=setAsPrinted&invoiceDate=<%= qInvoiceDate %>&companyID=<%= companyID %>';
	}
	else {
		return false;
	}
  }
  </script>
<%
    If IsDate(qInvoiceDate) Then 
        dbOpen()
        RecSet = Conn.Execute("Select co.countryID, co.company, co.debitAccount, co.creditAccount, co.uwDebitAccount, co.uwCreditAccount, co.roundingAccount, co.taxAccount, c.countryCurrency, c.decimals From (tblCompany co INNER JOIN tblCountry c ON c.countryID = co.countryID) Where co.companyID = " & companyID & "")
        If Not RecSet.EOF Then 
            countryID = RecSet.Fields("countryID").Value
            company = RecSet.Fields("company").Value
            debitAccount = RecSet.Fields("debitAccount").Value
            creditAccount = RecSet.Fields("creditAccount").Value
            uwDebitAccount = RecSet.Fields("uwDebitAccount").Value
            uwCreditAccount = RecSet.Fields("uwCreditAccount").Value
            roundingAccount = RecSet.Fields("roundingAccount").Value
            taxAccount = RecSet.Fields("taxAccount").Value
            countryCurrency = RecSet.Fields("countryCurrency").Value
            decimals = RecSet.Fields("decimals").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        OverPrint()
        Response.Write("<div class=noprint>")
        Response.Write("<table>")
        Response.Write("<tr>")
        Response.Write("<td class='headline'>")
        Response.Write("<form>")
        Response.Write("<input type='button' onClick='javascript:print();' value='Print'>&nbsp; ")
        Response.Write("<input type='button' value='Set invoice record as printed' onclick=""{return printCheck();} return false;"">")
        Response.Write("</form>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Response.Write("</div>")
        Response.Write("<p style='page-break-after: always'>")
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td colspan='3' class='printBoldHeadline'>Invoice record</td>")
        Response.Write("<td colspan='2' align='right' class='printBoldHeadline'>" & company & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td colspan='3' class='printBold'>Record draft up until " & qInvoiceDate & "</td>")
        Response.Write("<td colspan='2' align='right' class='printBold'>Page " & pageCounter & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td colspan='5'><hr></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printBold'>Invoice date</td>")
        Response.Write("<td class='printBold'>Invoice ID</td>")
        Response.Write("<td class='printBold'>Customer</td>")
        Response.Write("<td class='printBold'>Invoice amount (" & countryCurrency & ")<br>Account</td>")
        Response.Write("<td align='right' class='printBold'>Amount (" & countryCurrency & ")</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td colspan='5'><hr></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceID, i.invoiceDate, i.invoiceAmount, p.partnerID, p.partnerName, Sum(pl.uw) As uwSubTotal, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoice i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblInvoiceContent ic On ic.invoiceID = i.invoiceID) INNER JOIN tblPriceList pl ON pl.priceListID = ic.priceListID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND i.countryID = " & countryID & " AND i.invoiceRecord = 0 Group By i.invoiceID, i.invoiceDate, i.invoiceAmount, i.countryID, p.partnerID, p.partnerName Order By i.invoiceID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                uwSubTotal = RecSet.Fields("uwSubTotal").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Credit Invoice ****************
        RecSet = Conn.Execute("Select i.invoiceCreditID, i.invoiceDate, i.invoiceAmount, p.partnerID, p.partnerName, Sum(pl.uw) As uwSubTotal, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCredit i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblInvoiceCreditContent ic On ic.invoiceCreditID = i.invoiceCreditID) INNER JOIN tblPriceList pl ON pl.priceListID = ic.priceListID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND i.countryID = " & countryID & " AND i.invoiceRecord = 0 Group By i.invoiceCreditID, i.invoiceDate, i.invoiceAmount, i.countryID, p.partnerID, p.partnerName Order By i.invoiceCreditID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                uwSubTotal = RecSet.Fields("uwSubTotal").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = CDbl(debitGrandtotal) - CDbl(debitSubtotal)
                creditGrandtotal = CDbl(creditGrandtotal) - CDbl(creditSubtotal)
                roundingGrandtotal = roundingGrandtotal - roundingSubtotal
                taxGrandtotal = taxGrandtotal - taxSubtotal
                uwGrandTotal = CDbl(uwGrandTotal) - CDbl(uwSubTotal)
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceCreditID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Invoice due to price modification ****************
        RecSet = Conn.Execute("Select iplm.invoicePriceListModificationID, iplm.invoiceDate, iplm.invoiceAmount, p.partnerID, p.partnerName, Sum(pl1.uw) As oldUw, Sum(pl2.uw) As newUw, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = iplm.countryID And st.date <= iplm.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoicePriceListModification iplm INNER JOIN tblPartner p ON p.partnerID = iplm.partnerID) INNER JOIN tblInvoicePriceListModificationContent iplmc On iplmc.invoicePriceListModificationID = iplm.invoicePriceListModificationID) INNER JOIN tblPriceList pl1 ON pl1.priceListID = iplmc.oldPriceListID) INNER JOIN tblPriceList pl2 ON pl2.priceListID = iplmc.newPriceListID) Where iplm.invoiceDate <= '" & qInvoiceDate & "' AND iplm.countryID = " & countryID & " AND iplm.invoiceRecord = 0 Group By iplm.invoicePriceListModificationID, iplm.invoiceDate, iplm.invoiceAmount, iplm.countryID, p.partnerID, p.partnerName Order By iplm.invoicePriceListModificationID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoicePriceListModificationID = RecSet.Fields("invoicePriceListModificationID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                oldUw = RecSet.Fields("oldUw").Value
                newUw = RecSet.Fields("newUw").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                uwSubTotal = CDbl(newUw) - CDbl(oldUw)
                uwDebitSubTotal = uwSubTotal
                uwDebitSubTotal = FormatNumber(CDbl(uwDebitSubTotal), decimals)
                uwCreditSubTotal = CDbl(uwDebitSubTotal) * - 1
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                creditSubtotal = CDbl(creditSubtotal) * - 1
                taxSubtotal = taxSubtotal * - 1
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoicePriceListModificationID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwDebitSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(uwCreditSubTotal, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Penalty Invoice Duration ****************
        RecSet = Conn.Execute("Select i.invoiceDurationPenaltyID As invoiceID, i.invoiceDate, i.invoiceAmount, i.uw, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceDurationPenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                uwSubTotal = uw
                uwSubTotal = FormatNumber(CDbl(uwSubTotal), decimals)
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Credit Penalty Invoice Duration ****************
        RecSet = Conn.Execute("Select i.invoiceCreditDurationPenaltyID As invoiceID, i.invoiceDate, i.invoiceAmount, i.uw, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceCreditDurationPenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                uwSubTotal = uw
                uwSubTotal = FormatNumber(CDbl(uwSubTotal), decimals)
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = CDbl(debitGrandtotal) - CDbl(debitSubtotal)
                creditGrandtotal = CDbl(creditGrandtotal) - CDbl(creditSubtotal)
                roundingGrandtotal = roundingGrandtotal - roundingSubtotal
                taxGrandtotal = taxGrandtotal - taxSubtotal
                uwGrandTotal = CDbl(uwGrandTotal) - CDbl(uwSubTotal)
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Penalty Invoice Distance ****************
        RecSet = Conn.Execute("Select i.invoiceDistancePenaltyID As invoiceID, i.invoiceDate, i.invoiceAmount, i.uw, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceDistancePenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                uwSubTotal = uw
                uwSubTotal = FormatNumber(CDbl(uwSubTotal), decimals)
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Credit Penalty Invoice Distance ****************
        RecSet = Conn.Execute("Select i.invoiceCreditDistancePenaltyID As invoiceID, i.invoiceDate, i.invoiceAmount, i.uw, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceCreditDistancePenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                uwSubTotal = uw
                uwSubTotal = FormatNumber(CDbl(uwSubTotal), decimals)
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = CDbl(debitGrandtotal) - CDbl(debitSubtotal)
                creditGrandtotal = CDbl(creditGrandtotal) - CDbl(creditSubtotal)
                roundingGrandtotal = roundingGrandtotal - roundingSubtotal
                taxGrandtotal = taxGrandtotal - taxSubtotal
                uwGrandTotal = CDbl(uwGrandTotal) - CDbl(uwSubTotal)
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Service Account Balance Invoice ****************
        RecSet = Conn.Execute("Select i.invoiceServiceAccountBalanceID As invoiceID, i.invoiceDate, i.invoiceAmount, i.uw, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceServiceAccountBalanceID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                uwSubTotal = uw
                uwSubTotal = FormatNumber(CDbl(uwSubTotal), decimals)
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Credit Service Account Balance Invoice ****************
        RecSet = Conn.Execute("Select i.invoiceCreditServiceAccountBalanceID As invoiceID, i.invoiceDate, i.invoiceAmount, i.uw, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceCreditServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceCreditServiceAccountBalanceID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                uwSubTotal = uw
                uwSubTotal = FormatNumber(CDbl(uwSubTotal), decimals)
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Admin Fee Invoice ****************
        RecSet = Conn.Execute("Select i.invoiceAdminFeeID As invoiceID, i.invoiceDate, i.invoiceAmount, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceAdminFee i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND i.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceAdminFeeID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                uwSubTotal = 0
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Separate Invoice ****************
        RecSet = Conn.Execute("Select i.invoiceSeparateID As invoiceID, i.invoiceDate, i.invoiceAmount, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceSeparate i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND i.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceSeparateID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                uwSubTotal = 0
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = debitGrandtotal + debitSubtotal
                creditGrandtotal = creditGrandtotal + creditSubtotal
                roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                taxGrandtotal = taxGrandtotal + taxSubtotal
                uwGrandTotal = uwGrandTotal + uwSubTotal
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        Response.Flush()
        '*************** Separate Credit Invoice ****************
        RecSet = Conn.Execute("Select i.invoiceCreditSeparateID As invoiceID, i.invoiceDate, i.invoiceAmount, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceCreditSeparate i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.invoiceDate <= '" & qInvoiceDate & "' AND i.countryID = " & countryID & " AND i.invoiceRecord = 0 Order By i.invoiceCreditSeparateID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                PageHeader()
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                salestax = RecSet.Fields("salesTax").Value
                uwSubTotal = 0
                creditSubtotal = invoiceAmount
                taxSubtotal = (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                invoiceAmount = invoiceAmount + taxSubtotal
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                debitSubtotal = invoiceAmount
                roundingSubtotal = 0
                debitGrandtotal = CDbl(debitGrandtotal) - CDbl(debitSubtotal)
                creditGrandtotal = CDbl(creditGrandtotal) - CDbl(creditSubtotal)
                roundingGrandtotal = roundingGrandtotal - roundingSubtotal
                taxGrandtotal = taxGrandtotal - taxSubtotal
                uwGrandTotal = CDbl(uwGrandTotal) - CDbl(uwSubTotal)
                Response.Write("<tr>")
                Response.Write("<td class='printRegular'>" & invoiceDate & "</td>")
                Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                If roundingSubtotal > 0 Then 
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingSubtotal * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                End IF
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditSubtotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxSubtotal, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal) * - 1, 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td colspan='3'></td>")
                Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
                Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwSubTotal), 2) & "</td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                debitSubtotal = 0
                creditSubtotal = 0
                roundingSubtotal = 0
                taxSubtotal = 0
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Flush()
        Response.Write("</table>")
        Response.Write("</p>")
        creditGrandtotal = CDbl(creditGrandtotal) * - 1
        uwDebitGrandTotal = uwGrandTotal
        uwCreditGrandTotal = CDbl(uwDebitGrandTotal) * - 1
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td class='printBoldHeadline'>Invoice record</td>")
        Response.Write("<td align='right' class='printBoldHeadline'>" & company & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printBold'>Record draft up until " & qInvoiceDate & "</td>")
        Response.Write("<td align='right' class='printBold'>Page 1 of " & pageCounter & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td colspan='2'><hr></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printBold'>Account</td>")
        Response.Write("<td align='right' class='printBold'>Amount (" & countryCurrency & ")</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td colspan='2'><hr></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitGrandtotal), 2) & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printRegular'>" & creditAccount & "</td>")
        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(creditGrandtotal), 2) & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printRegular'>" & uwDebitAccount & "</td>")
        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(uwDebitGrandTotal), 2) & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printRegular'>" & uwCreditAccount & "</td>")
        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(uwCreditGrandTotal, 2) & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(roundingGrandtotal, 2) & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='printRegular'>" & taxAccount & "</td>")
        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(taxGrandtotal * - 1, 2) & "</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td colspan='2'><hr></td></tr>")
        Response.Write("</table>")
        Under()
    Else
        OverPrint()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr><td class='printBoldHeadline'><font color='red'>The latest invoice date " & qInvoiceDate & " is not a valid date.</font></td></tr>")
        Response.Write("</table>")
        Under()
    End IF
ElseIf Request.QueryString("do") = "setAsPrinted" Then 
    invoiceDate = Request.QueryString("invoiceDate")
    companyID = Request.QueryString("companyID")
    companyID = CLng(companyID)
    dbOpen()
    RecSet = Conn.Execute("Select countryID From tblCompany Where companyID = " & companyID & "")
    countryID = RecSet.Fields("countryID").Value
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    dbOpen()
    Conn.Execute("Update tblInvoice Set invoiceRecord = 1 Where invoiceDate <= '" & invoiceDate & "' AND countryID = " & countryID & " AND invoiceRecord = 0")
    dbClose()
    dbOpen()
    Conn.Execute("Update tblInvoiceCredit Set invoiceRecord = 1 Where invoiceDate <= '" & invoiceDate & "' AND countryID = " & countryID & " AND invoiceRecord = 0")
    dbClose()
    dbOpen()
    Conn.Execute("Update tblInvoicePriceListModification Set invoiceRecord = 1 Where invoiceDate <= '" & invoiceDate & "' AND countryID = " & countryID & " AND invoiceRecord = 0")
    dbClose()
    dbOpen()
    RecSet = Conn.Execute("Select i.invoiceDurationPenaltyID As invoiceID From (tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate <= '" & invoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0")
    If Not RecSet.EOF Then 
        Do Until RecSet.EOF
            invoiceID = RecSet.Fields("invoiceID").Value
            Conn.Execute("Update tblInvoiceDurationPenalty Set invoiceRecord = 1 Where invoiceDurationPenaltyID = " & invoiceID & "")
            RecSet.MoveNext()
        Loop
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    dbOpen()
    RecSet = Conn.Execute("Select i.invoiceCreditDurationPenaltyID As invoiceID From (tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate <= '" & invoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0")
    If Not RecSet.EOF Then 
        Do Until RecSet.EOF
            invoiceID = RecSet.Fields("invoiceID").Value
            Conn.Execute("Update tblInvoiceCreditDurationPenalty Set invoiceRecord = 1 Where invoiceCreditDurationPenaltyID = " & invoiceID & "")
            RecSet.MoveNext()
        Loop
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    dbOpen()
    RecSet = Conn.Execute("Select i.invoiceDistancePenaltyID As invoiceID From (tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate <= '" & invoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0")
    If Not RecSet.EOF Then 
        Do Until RecSet.EOF
            invoiceID = RecSet.Fields("invoiceID").Value
            Conn.Execute("Update tblInvoiceDistancePenalty Set invoiceRecord = 1 Where invoiceDistancePenaltyID = " & invoiceID & "")
            RecSet.MoveNext()
        Loop
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    dbOpen()
    RecSet = Conn.Execute("Select i.invoiceCreditDistancePenaltyID As invoiceID From (tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate <= '" & invoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0")
    If Not RecSet.EOF Then 
        Do Until RecSet.EOF
            invoiceID = RecSet.Fields("invoiceID").Value
            Conn.Execute("Update tblInvoiceCreditDistancePenalty Set invoiceRecord = 1 Where invoiceCreditDistancePenaltyID = " & invoiceID & "")
            RecSet.MoveNext()
        Loop
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    dbOpen()
    RecSet = Conn.Execute("Select i.invoiceServiceAccountBalanceID As invoiceID From (tblInvoiceServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate <= '" & invoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0")
    If Not RecSet.EOF Then 
        Do Until RecSet.EOF
            invoiceID = RecSet.Fields("invoiceID").Value
            Conn.Execute("Update tblInvoiceServiceAccountBalance Set invoiceRecord = 1 Where invoiceServiceAccountBalanceID = " & invoiceID & "")
            RecSet.MoveNext()
        Loop
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    dbOpen()
    RecSet = Conn.Execute("Select i.invoiceCreditServiceAccountBalanceID As invoiceID From (tblInvoiceCreditServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate <= '" & invoiceDate & "' AND v.countryID = " & countryID & " AND i.invoiceRecord = 0")
    If Not RecSet.EOF Then 
        Do Until RecSet.EOF
            invoiceID = RecSet.Fields("invoiceID").Value
            Conn.Execute("Update tblInvoiceCreditServiceAccountBalance Set invoiceRecord = 1 Where invoiceCreditServiceAccountBalanceID = " & invoiceID & "")
            RecSet.MoveNext()
        Loop
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    dbOpen()
    Conn.Execute("Update tblInvoiceAdminFee Set invoiceRecord = 1 Where invoiceDate <= '" & invoiceDate & "' AND countryID = " & countryID & " AND invoiceRecord = 0")
    dbClose()
    dbOpen()
    Conn.Execute("Update tblInvoiceSeparate Set invoiceRecord = 1 Where invoiceDate <= '" & invoiceDate & "' AND countryID = " & countryID & " AND invoiceRecord = 0")
    dbClose()
    dbOpen()
    Conn.Execute("Update tblInvoiceCreditSeparate Set invoiceRecord = 1 Where invoiceDate <= '" & invoiceDate & "' AND countryID = " & countryID & " AND invoiceRecord = 0")
    dbClose()
%>
  <script>
    self.close()
  </script>
<%
End IF
%>
